'use strict'

define([ 'jquery', 'bootstrap', 'select2', 'shiftform', 'validate',
        'validate.en'], function($) {
    return {
        init : function() {
            
            this.initSearch();

        },
        
        initSearch: function() {
        	var that = this;
            $(document).on('shown.bs.modal', '#search', function(e){
                
                $('select[name="session"]').on("change", function() {
                	var thisSelect = this;
                    var session = $('select[name="session"]').val();
                    if(session) {
                        $.ajax({
                    		url: window.__CTX__.root + '/session-reports/delegates',
                    		type: 'POST',
                    		data: {'sessionId': session},
                    		success: function(res) {
                                if(res.code == 0) {
                                    var delegates = res.data.delegates;
                                    $('select[name="delegate"]').find("option").remove();
                                    
                                    delegates.map(function(val,index,arr){
                                    	if(index == 0) {
                                    		$('select[name="delegate"]').append("<option value=''></option>");
                                    	}
                                	    $('select[name="delegate"]').append("<option value='"+val.id+"'>"+val.name+"</option>");
                                	})
                                    
                                } 
                            }
                    		
                    	})
                        
                    }
                })
                
                $('.search-submit').on("click", function() {
                	var session = $('select[name="session"]').val();
                    var delegate = $('select[name="delegate"]').val();
                	if(!session && !delegate) {
                		$('.alert-danger').removeClass("hide");
                		$('.alert-danger').html('<i class="glyphicon glyphicon-remove"></i>session或delegate不可为空')
                	} else {
                		$('.ms-form').submit();
                	}
                	
                })
                
              })
        }
    

    }
})
